<template>
  <div>
    <el-submenu v-if="menu.subMenus && menu.subMenus.length >= 1" :index="menu.id + ''"
     :popper-class="'site-sidebar--' + sidebarLayoutSkin + '-popper'">
      <template slot="title">
        <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
        <span class="site-sidebar__menu-span">{{ menu.name }}<el-badge v-if="menu.fresh" value="new"></el-badge></span>
      </template>
      <main-sidebar-menu v-for="item in menu.subMenus" :key="item.id" :menu="item" :dynamicMenuRoutes="dynamicMenuRoutes"></main-sidebar-menu>
    </el-submenu>
    <el-menu-item v-else :index="menu.id + ''" @click="gotoRouteHandle(menu)">
      <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
      <span class="site-sidebar__menu-span">{{ menu.name }}<el-badge v-if="menu.fresh" value="new"></el-badge></span>
    </el-menu-item>
  </div>
</template>

<script>
import MainSidebarMenu from './main-sidebar-menu'
export default {
  name: 'main-sidebar-menu',
  props: {
    menu: {
      type: Object,
      required: true
    },
    dynamicMenuRoutes: {
      type: Array,
      required: true
    }
  },
  components: {
    MainSidebarMenu
  },
  computed: {
    sidebarLayoutSkin: {
      get() {
        return this.$store.getters.sidebarLayoutSkin
      }
    }
  },
  methods: {
    // 通过menuId与动态(菜单)路由进行匹配跳转至指定路由
    gotoRouteHandle(menu) {
      let route = this.dynamicMenuRoutes.filter(item => item.meta.menuId === menu.id)
      if (route.length >= 1) {
        this.$router.push({ name: route[0].name })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.site-sidebar {
  &__inner {
    span.site-sidebar__menu-span {
      display: inline-block;
      width: 100px;
      text-align: left;
    }
    .el-menu-item {
      padding-right: 20px
    }
  }
}
.el-menu--collapse {
  > div {
    > .el-submenu {
      > .el-submenu__title {
        span {
          height: 0;
          width: 0;
          overflow: hidden;
          visibility: hidden;
          display: inline-block;
        }
        .el-submenu__icon-arrow {
          display: none;
        }
      }
    }
  }
}
</style>
